package com.pym.bs.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pym.bs.dao.LdemoMapper;
import com.pym.bs.domain.Ldemo;
import com.pym.bs.dto.DemosDTO;
import com.pym.bs.dto.LdemoDTO;
import com.pym.bs.service.LdemoService;
import com.pym.bs.utils.PageVOUtils;
import com.pym.bs.utils.Pageable;
import com.pym.bs.utils.ResultVOUtils;
import com.pym.bs.vo.PageVO;
import com.pym.bs.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Service
public class LdemoServiceImpl implements LdemoService {
    
    @Resource
    private LdemoMapper ldemoMapper;
    
    @Override
    public ResultVO<Ldemo> save(LdemoDTO ldemoDTO) throws IOException {
        Ldemo ldemo = new Ldemo();
        Date date = new Date();
        ldemoDTO.setCreateTime(date);
        BeanUtils.copyProperties(ldemoDTO, ldemo);
        ldemoMapper.insertSelective(ldemo);
        return ResultVOUtils.success();
    }

    @Override
    public PageVO<DemosDTO> list(LdemoDTO ldemoDTO, Pageable pageable) {
        PageHelper.startPage(pageable);
        if (ldemoDTO.getSelect() != "") {
            ldemoDTO.setWords(ldemoDTO.getSelect());
        }
        Page<DemosDTO> list = ldemoMapper.selectByKeyWords(ldemoDTO);
        return PageVOUtils.of(list.getResult(), list.getTotal());
    }
}
